var app = getApp().$wepy;
import { getCourse } from '@/apis/courseApi';
const ORDER_ITEM = [
    {
        title: '默认',
        text:'默认',
        orderIcon: false,
        value: '',
    },
    {
        title: '最新',
        text:'最新',
        orderIcon: true,
        value: 'create_time',
    },
    {
        title: '学员人数',
        text: '学员人数',
        orderIcon: true,
        value: 'student_count',
    },
    {
        title: '点击量',
        text: '点击量',
        orderIcon: true,
        value: 'click_count',
    },
];
const PAGE_SIZE = 10;
export default {
    data: {
        courseList: [],
        showRefresh: false, //是否刷新完成，显示和隐藏刷新效果
        showLoading: false, //是否显示分页加载loading效果
        loading: false,
        orderIndex: 0,        
        isAsc: true,
        orderItems: ORDER_ITEM,
        currentPage: 1,
        pageSize: PAGE_SIZE,
        totalPage: 1,
        totalCount: 0,
        startHours:undefined,
        endHours:undefined,
        startCommentScore:undefined,
        endCommentScore:undefined,
        name:undefined
    },
    computed: {
        sortKey() {
            return this.isAsc ? 'asc' : 'desc';
        },
        noMore() {
            return this.currentPage > this.totalPage;
        },
    },
    methods: {
        _getCourse(callback) {
            var _this = this.$wx;            
            var params = {
                page: this.currentPage,
                size: this.pageSize,
                sortKey:this.sort?this.sort:this.orderItems[this.orderIndex].value,
                sortDirection: this.sortKey,
                typeId: this.typeId,//页面自己定义
                startHours:this.startHours,//页面自己定义
                endHours:this.endHours,//页面自己定义
                startCommentScore:this.startCommentScore,//页面自己定义
                endCommentScore:this.endCommentScore,//页面自己定义
                name:this.name//页面自己定义
            };
            for(var o in params){                
                if(params[o]==undefined||params[o]==null||params[o]==''){
                    delete params[o];
                }
            }
            var res = getCourse(params)
                .then((res) => {
                    var data = res.data.data;
                    if (app.handleResponse(res)) {
                        if (this.currentPage === 1) {
                            this.courseList = this._formatCourseList(data.list);
                            this.totalCount = data.totalCount;
                            this.totalPage = data.totalPage;
                        } else {
                            this.courseList = [
                                ...this.courseList,
                                ...this._formatCourseList(data.list),
                            ];
                        }
                        this.currentPage = this.currentPage + 1;
                    }
                })
                .catch((msg) => {
                    app.showError(msg);
                })
                .finally(() => {
                    callback && callback();
                });
        },
        _formatCourseList(list) {
            return list.map((x) => {
                return {
                    image: x.cover,
                    desc: `${x.typeName} / ${x.hours?x.hours:'-'}周 / ${x.credit?x.credit:'-'}学分`,
                    count: x.studentCount,
                    flag: x.label,
                    title: x.name,
                    typeId: x.typeId,
                    id:x.id
                };
            });
        },
        onRefresh(callback) {
            if (this.showRefresh) return;
            this.showRefresh = true;
            this.currentPage = 1;
            this._getCourse(() => {
                this.showRefresh = false;
            });
        },
        _updateRefreshStatus(status = false) {
            this.showRefresh = status;
        },
        _updateLoadingStatus(status = false) {
            this.showLoading = status;
        },
        onScrollLower(e) {
            if (this.noMore) return;
            if (this.showLoading) return;
            this.showLoading = true;
            this._getCourse(() => {
                this.showLoading = false;
            });
        },
    }
}
